package com.ashleyleung.jetpack_b_station.taobao

import android.graphics.Rect
import android.os.Bundle
import android.view.View
import android.view.View.GONE
import android.view.View.VISIBLE
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.RecyclerView
import com.ashleyleung.jetpack_b_station.R
import com.ashleyleung.jetpack_b_station.base.LoadState
import com.ashleyleung.jetpack_b_station.utils.SizeUtils
import com.ashleyleung.jetpack_b_station.utils.ViewInitUtil
import com.ashleyleung.jetpack_b_station.utils.longToast
import com.ashleyleung.jetpack_b_station.utils.toast
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import kotlinx.android.synthetic.main.activity_on_error.*
import kotlinx.android.synthetic.main.activity_on_sell.*
import kotlinx.android.synthetic.main.activity_on_sell.view.*

/**
 * @author lxy
 * @name
 * @class describe  https://www.sunofbeach.net/a/1201366916766224384  阳光沙滩商城的API文档
 * @date 2022-06-28
 * Copyright (c)  Guangzhou GCI Science & Technology Co., Ltd.All rights reserved
 */
class OnSellActivity : AppCompatActivity() {

    private val mViewModel by lazy {
        ViewModelProvider(this).get(OnSellViewModel::class.java)
    }

    private val mAdapter by lazy {
        OnSellListAdapter()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_on_sell)
        initView()
        initObserver()
    }

    private fun initObserver() {
        mViewModel.apply {
            contentList.observe(this@OnSellActivity, Observer {
                //内容列表更新
                mAdapter.setData(it)
            })
            loadState.observe(this@OnSellActivity, Observer {
//根据加载的状态来更新uT的显示
                if(it!= LoadState.LOAD_MORE_LOADING )
                hideAll()
                when (it) {
                    LoadState.LOADING -> {//首次加载，列表为空
                        loadingView.visibility = VISIBLE
                    }
//          写了  if(it!= LoadState.LOAD_MORE_LOADING )就不用写下面的
//                    LoadState.LOAD_MORE_LOADING -> {
//                        contentListRv.visibility = VISIBLE
//                    }
                    LoadState.EMPTY -> {
                        emptyView.visibility = VISIBLE
                        ViewInitUtil.resetSmartRefreshLayout(smartRefreshLayout)
                    }
                    LoadState.LOAD_MORE_EMPTY -> {
                        longToast(this@OnSellActivity, "已经加载全部内容了")
                        ViewInitUtil.resetSmartRefreshLayout(smartRefreshLayout)
                    }
                    LoadState.ERROR -> {
                        errorView.visibility = VISIBLE
                        ViewInitUtil.resetSmartRefreshLayout(smartRefreshLayout)
                        toast(this@OnSellActivity, "网络不佳")
                    }
                    LoadState.SUCCESS -> {
                        toast(this@OnSellActivity, "加载成功")
                        contentListRv.visibility = VISIBLE
                        ViewInitUtil.resetSmartRefreshLayout(smartRefreshLayout)
                    }
                    else -> {}
                }
            })
        }.loadContent()
    }


    private fun initView() {
        reloadLl.setOnClickListener {
            //重新加载
            mViewModel.loadContent()
        }
        ViewInitUtil.initSmartRefreshLayout(this@OnSellActivity, smartRefreshLayout)
        ViewInitUtil.initLinearRecyclerView(this@OnSellActivity, contentListRv)
        smartRefreshLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onLoadMore(refreshLayout: RefreshLayout) {
                mViewModel.loadMore()
            }

            override fun onRefresh(refreshLayout: RefreshLayout) {
                mViewModel.refresh()
            }
        })

        contentListRv.run {
            adapter = mAdapter
            addItemDecoration(object : RecyclerView.ItemDecoration() {
                override fun getItemOffsets(
                    outRect: Rect,
                    view: View,
                    parent: RecyclerView,
                    state: RecyclerView.State
                ) {

                    outRect.apply {
                        var padding = SizeUtils.dip2px(this@OnSellActivity, 8.0f)
                        top = padding
                        left = padding
                        right = padding
                    }

//                    outRect.top = 8
//                    outRect.left = 8
//                    outRect.right = 8
//                    outRect.bottom = 8
                }
            })
        }
    }

    private fun hideAll() {
        contentListRv.visibility = GONE
        emptyView.visibility = GONE
        loadingView.visibility = GONE
        errorView.visibility = GONE
    }

}